Ext.define('cloud.controller.Main', {
    extend: 'Ext.app.Controller',
    config: {
        // viewCache: [],
        refs: {
            main: 'main',
            nav: 'main #mainNestedList',
            sourceButton: 'button[action=viewSource]',
            area_id: 'button[action=area_id]',
            selectAreaByApp: 'main #selectAreaByApp',
            sourceOverlay: {
                selector: 'sourceoverlay',
                xtype: 'sourceoverlay',
                autoCreate: true
            }
        },
        control: {
            sourceButton: {
                tap: 'onSourceTap'
            },
            nav: {
                itemtap: 'onNavTap',
                selectionchange: function (list, selections, eOpts) {
                        var id = eOpts[0].data.id;
                        var lastid = this.getLeaveid();
                        if (document.getElementById(id) != null) {
                            if (id != lastid) {
                                document.getElementById(id).className = 'icon1-' + id;
                                if (document.getElementById(lastid) != null || document.getElementById(lastid) != undefined) {
                                    document.getElementById(lastid).className = 'icon-' + lastid;
                                }
                            }
                        this.setLeaveid(id);
                    }
                }
            },
            selectAreaByApp: {
                change: function (select, newValue, oldValue) {
                    this.getMain().removeAll(true, false);
                    this.getMain().add({xtype: 'welcome'});
                    this.setCurrentTree(undefined);
                    this.setLeaveid(undefined);
                    var cadid = newValue;
                    var tree = Ext.getStore('Tree');
                    tree.load({params: {
                            username: localStorage.getItem('username'),
                            caid: localStorage.getItem('caid'),
                            cadid: cadid
                        }, callback: function (data) {
                            localStorage.setItem('cadid', cadid);
                            Ext.getCmp('mainNestedList').setStore(tree);
                        }, scope: this}
                    );
                }
            }
        },
        routes: {
            'tree/:id': 'showViewById'
        },
        currentTree: undefined,
        leaveid: undefined,
        selectid: undefined
    },
    showViewById: function (id) {
        var nav = this.getNav();
        if (nav.getStore() != null) {
            var view = nav.getStore().getNodeById(id);
            var name = this.getViewName(view);
            var oldName = this.getCurrentTree();
            if (name != oldName) {
                this.showView(view);
                this.hideSheets();
            }
        }
    },
    showView: function (item) {
        var nav = this.getNav(),
            view = this.createView(item),
            main = this.getMain(),
            layout = main.getLayout(),
            initialAnim = layout.getAnimation();
        main.removeAll(true, false);
        nav.setDetailContainer(main);
        nav.setDetailCard(view);
        nav.goToNode(item.parentNode);
        nav.goToLeaf(item);
        nav.getActiveItem().select(item);
    },
    createView: function (item) {
        var name = this.getViewName(item);
        this.setCurrentTree(name);
        var view = Ext.create(name);
        view.viewName = name;
        return view;
    },
    /**
     * Shows the source code for the {@link #currentDemo} in an overlay
     */
    onNavTap: function (nestedList, list, index) {
        var record = list.getStore().getAt(index);
        if (record.isLeaf()) {
            this.redirectTo(record);
        }
    },

    getViewName: function (item) {
     //   console.log(item.get('id') || item.get('text'))
        if (item.get('parentId') != 'Tree-root') {
            var name = item.get('id') || item.get('text'),
                ns = 'cloud.view.' + item.get('parentId') + '.';

        } else {
            var name = item.get('id') || item.get('text'),
                ns = 'cloud.view.' + item.get('id').toLocaleLowerCase() + '.';
        }
        return ns + name;
    },

    /**
     * we iterate over all of the floating sheet components and make sure they're hidden when we
     * navigate to a new view. This stops things like Picker overlays staying visible when you hit
     * the browser's back button
     */
    hideSheets: function () {
        Ext.each(Ext.ComponentQuery.query('sheet'), function (sheet) {
            sheet.setHidden(true);
        });
    }
});